package com.cv.framework.websocket.core.security;

import cn.hutool.core.util.ObjUtil;
import com.cv.framework.security.core.utils.SecurityFrameworkUtil;
import com.cv.framework.websocket.core.utils.WebSocketFrameworkUtil;
import com.cv.system.api.token.form.LoginUser;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import java.util.Map;

/**
 * 拼接登录的 Token {@link org.springframework.web.socket.server.HandshakeInterceptor}
 *
 * @author Charles_XDXD
 */
public class LoginUserHandshakeInterceptor implements HandshakeInterceptor {

    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
                                   WebSocketHandler wsHandler, Map<String, Object> attributes) {
        LoginUser loginUser = SecurityFrameworkUtil.getLoginUser();
        if (ObjUtil.isNotEmpty(loginUser)) {
            WebSocketFrameworkUtil.setLoginUser(loginUser, attributes);
        }
        return true;
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response,
                               WebSocketHandler wsHandler, Exception exception) {

    }

}
